/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.channel.service.impl.ChannelServiceImpl
   Module Description   :

   Date Created      : 2010/7/5
   Original Author   : feeling
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.channel.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.dao.ObjectDao;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.ec.channel.entity.Channel;
import com.yaodian100.ec.channel.entity.ChannelTemplate;
import com.yaodian100.ec.channel.service.ChannelService;
import com.yaodian100.ec.common.EcConstant;

/**
 * @author feeling
 * 
 */
public class ChannelServiceImpl extends DomainServiceImpl<Channel> implements ChannelService {
	String[] pattern_date = new String[] { EcConstant.PATTERN_DATE };

	private ObjectDao<ChannelTemplate> channelTemplateDao;

	@Override
	public Long getActivateChannelIdByName(String channelName) throws Exception {
		CommonCriteria comm = new CommonCriteria();
		comm.addEq("channelName", channelName);
		comm.addLe("startDate", new Date());
		comm.addGe("endDate", new Date());
		List<Channel> cList = getDao().getList(comm, null);
		if (null != cList && cList.size() > 0)
			return cList.get(0).getId();
		return Long.parseLong("0");
	}

	@Override
	public List<ChannelTemplate> getAvailableChannelTemplate(String channelName) {
		CommonCriteria criteria = new CommonCriteria();
		criteria.addEq("channelName", channelName);
		criteria.addGe("endDate", new Date());
		criteria.addEq("channelStatus", "Y");
		List<ChannelTemplate> result = getChannelTemplateDao().getList(criteria, null);
		if ((null != result) && (result.size() > 0))
			return result;
		return null;
	}

	@Override
	public ChannelTemplate getAvailableChannelTemplate(String channelName, String type) {
		CommonCriteria criteria = new CommonCriteria();
		criteria.addEq("channelName", channelName);
		criteria.addEq("type", type);
		// criteria.addLe("startDate", new Date());
		criteria.addGe("endDate", new Date());
		criteria.addEq("channelStatus", "Y");
		List<ChannelTemplate> result = channelTemplateDao.getList(criteria, null);
		if ((null != result) && (result.size() > 0))
			return result.get(0);
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.channel.service.ChannelService#getChannelByCondication(java.lang.String, java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public int getChannelByCondication(String channelName, String actDate, String actType, String channelStatus)
			throws Exception {
		StringBuilder queryString = new StringBuilder();
		List<Serializable> attrs = new ArrayList<Serializable>();
		queryString.append("select count(*) from Channel where 1 = 1 ");
		if (StringUtils.isNotBlank(channelName)) {
			queryString.append("and channelName = ? ");
			attrs.add(channelName);
		}
		if (StringUtils.isNotBlank(actDate)) {
			queryString.append("and ? between startDate and endDate ");
			attrs.add(DateUtils.parseDate(actDate, pattern_date));
		}
		if (StringUtils.isNotBlank(actType)) {
			queryString.append("and actType = ? ");
			attrs.add(actType);
		}
		if (!StringUtils.equalsIgnoreCase("A", channelStatus)) {
			queryString.append("and channelStatus = ? ");
			attrs.add(channelStatus);
		}
		List channelList = getDao().getQueryByList(queryString.toString(), attrs, 0, -1);
		return Integer.parseInt(channelList.get(0).toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.channel.service.ChannelService#getChannelByCondication(java.lang.String, java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public List<Channel> getChannelByCondication(String channelName, String actDate, String actType,
			String channelStatus, int firstResult, int maxResults) throws Exception {
		StringBuilder queryString = new StringBuilder();
		List<Serializable> attrs = new ArrayList<Serializable>();
		queryString.append("from Channel where 1 = 1 ");
		if (StringUtils.isNotBlank(channelName)) {
			queryString.append("and channelName = ? ");
			attrs.add(channelName);
		}
		if (StringUtils.isNotBlank(actDate)) {
			queryString.append("and ? between startDate and endDate ");
			attrs.add(DateUtils.parseDate(actDate, pattern_date));
		}
		if (StringUtils.isNotBlank(actType)) {
			queryString.append("and actType = ? ");
			attrs.add(actType);
		}
		if (!StringUtils.equalsIgnoreCase("A", channelStatus)) {
			queryString.append("and channelStatus = ? ");
			attrs.add(channelStatus);
		}
		List<Channel> channelList = getDao().getQueryByList(queryString.toString(), attrs, firstResult, maxResults);
		return channelList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.channel.service.ChannelService#getChannelTemplate(java.io.Serializable)
	 */
	@Override
	public ChannelTemplate getChannelTemplate(Serializable id) throws Exception {
		return channelTemplateDao.get(id);
	}

	public ObjectDao<ChannelTemplate> getChannelTemplateDao() {
		return channelTemplateDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.channel.service.ChannelService#getChannelTemplateListPageable(java.lang.String,
	 * java.lang.String, java.lang.String, java.lang.String, int, int)
	 */
	@Override
	public List<ChannelTemplate> getChannelTemplateListPageable(CommonCriteria criteria, String[] sortOrder,
			int firstResult, int maxResults) throws Exception {
		List<ChannelTemplate> templateLists = channelTemplateDao.getListPageable(criteria, new String[] { "endDate asc" },
				firstResult, maxResults);
		return templateLists;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.yaodian100.ec.channel.service.ChannelService#getChannelTemplateList(com.yaodian100.core.common.dao.impl.
	 * CommonCriteria)
	 */
	@Override
	public int getChannelTemplateListSize(CommonCriteria criteria) throws Exception {
		return channelTemplateDao.getListSize(criteria).intValue();
	}

	@Override
	@Transactional(readOnly = false)
	public ChannelTemplate saveChannelTemplate(ChannelTemplate entity) throws CoreException {
		return channelTemplateDao.saveOrUpdate(entity);
	}

	public void setChannelTemplateDao(ObjectDao<ChannelTemplate> channelTemplateDao) {
		this.channelTemplateDao = channelTemplateDao;
	}

	@Override
	public Map<String, String> splitChild(String child) {
		Map<String, String> ch_child = new HashMap<String, String>();
		if (StringUtils.isNotBlank(child)) {
			String spdata[] = StringUtils.split(child, "&");
			for (int i = 0; i < spdata.length; i++) {
				String[] tmp = StringUtils.split(spdata[i], "=");
				if (tmp.length > 1)
					ch_child.put(tmp[0], tmp[1]);
			}
		}
		return ch_child;
	}
}